
package com.tools.cleanmaster.controller;

import android.content.Context;

import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.model.TrashesData;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.io.File;
import java.util.HashSet;

public abstract class TrashScanner {
    static HashSet<String> PKGS_WHITE_LIST = new HashSet<String>();

    static {
        PKGS_WHITE_LIST.add(CleanConstant.REAL_PACKAGE_NAME);
    }

    protected static final String SUB_TAG = "TrashScanner";

    protected Context mContext;

    protected volatile boolean mStopRequested = false;

    protected boolean mIsScanning = false;

    protected TrashesData mTrashesData;

    protected TrashHandler mTrashHandler;

    public TrashScanner(Context context, TrashHandler handler) {
        mContext = context;
        mTrashHandler = handler;
        mTrashesData = mTrashHandler.getTrashesData();

    }

    public abstract void scan(int increaseProgress);

    public void stop() {
        mStopRequested = true;
        mIsScanning = false;
    }

    public static int[] calculateProgressPoint(int start, int end, int i, int N) {
        if (start >= end) {
            return new int[]{
                    start, end
            };
        }
        final int PROGRESS_SPAN = end - start;
        final int newStart = start + i * PROGRESS_SPAN / N;
        final int newEnd = start + (i + 1) * PROGRESS_SPAN / N;
        return new int[]{
                newStart, newEnd
        };
    }

    /**
     * @param totalProgress
     * @param i
     * @param N
     * @return 返回对应的进度数
     */
    public static int calculateProgress(int totalProgress, int i, int N) {
        if (totalProgress <= 0 || N <= 0 || i >= N) {
            return 0;
        }
        if (i < N - 1) {
            return ((i + 1) * totalProgress / N) - (i * totalProgress / N);
        } else {
            return totalProgress - (N - 1) * totalProgress / N;
        }
    }

    /**
     * 通过子文件数来分配paths的Progress
     *
     * @param dir              父目录，如果没有，则置为null
     * @param paths            要分配的路径
     * @param increaseProgress 要分配的进度总数
     * @return 返回按子文件数来分配的进度数组，与paths一一对应
     */
    public static int[] calculateProgressByChildsCount(String dir, String[] paths,
                                                       int increaseProgress) {
        if (paths == null || paths.length < 1 || increaseProgress == 0) {
            return null;
        }
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(SUB_TAG, "calculateProgressByChildsCount increaseProgress="
                    + increaseProgress + ",dirs.length=" + paths.length);
        }
        int[] progressDirs = new int[paths.length];
        if (paths.length == 1) {
            progressDirs[0] = increaseProgress;
            return progressDirs;
        }
        int totalChildFiles = 0;
        // 第一次循环，里面是子文件数
        for (int i = 0; i < paths.length; i++) {
            File file;
            if (dir == null) {
                file = new File(paths[i]);
            } else {
                file = new File(dir, paths[i]);
            }
            if (file.exists()) {
                if (file.isDirectory()) {
                    String[] childFiles = file.list();
                    if (childFiles != null && childFiles.length > 0) {
                        progressDirs[i] = childFiles.length;
                        totalChildFiles += progressDirs[i];
                    } else {// 空文件夹
                        progressDirs[i] = 1;
                        ++totalChildFiles;
                    }
                } else {
                    progressDirs[i] = 1;
                    ++totalChildFiles;
                }
            } else {
                progressDirs[i] = 0;
            }
        }
        // 第二次循环计算每个文件的权重，最后一个保证把剩余的进度填满
        int progress_N_1 = 0;
        for (int i = 0; i < paths.length - 1; i++) {
            progressDirs[i] = progressDirs[i] * increaseProgress / totalChildFiles;
            progress_N_1 += progressDirs[i];
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(SUB_TAG, "calculateProgressByChildsCount result: dirs[i]=" + paths[i]
                        + ",progressDirs[i]=" + progressDirs[i]);
            }
        }
        progressDirs[paths.length - 1] = (increaseProgress - progress_N_1);
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(SUB_TAG, "calculateProgressByChildsCount result: dirs[i]="
                    + paths[paths.length - 1] + ",progressDirs[len]="
                    + progressDirs[paths.length - 1]);
        }
        return progressDirs;
    }

    /**
     * @return 是否正在扫描中
     */
    public boolean isScanning() {
        return mIsScanning;
    }

}
